<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\CouponRepo;



class CouponController extends Controller
{
    public function index(Request $request)
    {
    	$where = [];    	
       
    	$list = CouponRepo::pages($where);
    	
    	return view('admin.coupon.index',compact('list','where'));
    }


    public function create(Request $request)
    {      
        
        return view('admin.coupon.add');
    }

    public function save(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $num = (int)$request->num;
        for($i=0;$i<$num;$i++){
            $data['name'] = $request->name;
            $data['code'] = 'C'.date('ymd').mt_rand(10,99).str_random(2);
            $data['point'] = (int)$request->point;  
            $flag = CouponRepo::create($data);  
        }
        
        
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/coupon/')->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }

    public function edit($id)
    {
        $data = CouponRepo::find(['id'=>$id]);
        return view('admin.coupon.edit',compact('data'));
    }

    public function update(Request $request)
    {
        
        $rs['status'] = 'success';
        $where['id']  = $request->id;
        $data = $request->except(['_token','id']);
        //dd($data);
        //$data['price'] = $data['price']*100;
       
        $flag = CouponRepo::update($where,$data);
        return redirect('zadmin/coupon/')->with('rs',$rs);
    }

    public function del($id)
    {
        $rs['status'] = 'success';
        CouponRepo::destroy($id);
        return redirect('zadmin/product/')->with('rs',$rs);
    }

   

   
}
